<%@ page import="java.sql.SQLException" %>
<%@ page import="admin.Connectmysql" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.time.LocalDate" %><%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2025/6/22
  Time: 10:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    response.setCharacterEncoding("UTF-8");

    int id = Integer.parseInt(request.getParameter("class_no"));
    String name = request.getParameter("class_name");
    String headTeacher = request.getParameter("headTeacher");
    String description = request.getParameter("description");
    Connectmysql cmysql = new Connectmysql(
            "jdbc:mysql://localhost:3306/exam1",
            "root",
            "root",
            "com.mysql.cj.jdbc.Driver");
    try {
        if(cmysql.open()){

            ResultSet rst= cmysql.executeSql(
                    "SELECT id FROM class WHERE id = ?;",
                    new Object[]{id}
            );
            if(rst!=null&&rst.next()){
                // ID已存在，返回错误信息
                System.out.println("id存在");
            }else{
                int rst2 = cmysql.executeSqlUpdate("INSERT INTO class (class_id,class_name, description,head_teacher,create_time) VALUES (?, ?,?, ?, ?);"
                        ,new Object[]{id,name,description,headTeacher, LocalDate.now().toString()}
                );
                if(rst2>0){
                    //添加成功
                    System.out.println("添加成功");
                }else {
                    //添加失败
                }


            }

            rst.close();

        }
    } catch (ClassNotFoundException f) {
        f.printStackTrace();
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
%>
</body>
</html>
